<?php
/**
 * Created by PhpStorm.
 * User: chengyu
 * Date: 2021/6/7 0007
 * Time: 19:45
 */

namespace app\api\controller\v1;

use app\backend\validate\NewsInfoValidate;
use app\BaseController;
use app\common\model\Nav as NavModel;
use app\common\model\Website;
use function Couchbase\defaultDecoder;

class NavWeb extends BaseController
{
    //程宇
    public function index()
    {
        return sucessMsg('200', '获取成功', [
            'topnav' => $this->topnav(),//获取顶部导航
            'buttopnav' => $this->buttopnav(),//获取底部上导航
            'butnav' => $this->butnav(),//获取底部下导航
            'web' => $this->web()//获取网站基础信息
        ]);
    }

    //获取顶部导航
    public function topnav()
    {
        $model = New NavModel();
        $topnav = $model->where(['status' => 1, 'type' => 1])
            ->field('id,name')
            ->order('sort', 'asc')
            ->select();
        return $topnav;
    }

    //获取底部上导航
    public function buttopnav()
    {
        $model = New NavModel();
        $nav = $model->where(['status' => 1, 'type' => 2])
            ->field('id,pid,image,name')
            ->order('sort', 'asc')
            ->select()->toArray();
        foreach($nav as &$v){
            $v['image'] = env('app.yuming') . $v['image'];
        }
        // 检验必须为数组
        if (!is_array($nav)) {
            return false;
        }
        $tree = [];
        foreach ($nav as $k => $v) {
            if ($v['pid'] == 0) {
                $tree[$k] = $v;
                $tree[$k]['children'] = getTree($nav, $v['id']);
            }
            unset($nav[$k]);
        }
        return $tree;
    }

    //获取底部下导航
    public function butnav()
    {
        $model = New NavModel();
        $butnav = $model->where(['status' => 1, 'type' => 3])
            ->field('id,name')
            ->order('sort', 'asc')
            ->select();
        return $butnav;
    }

    //获取网站基础信息
    public function web()
    {
        $model = New Website();
        $web = $model->where('status', 1)
            ->field('id,logo,english,name,app')
            ->find();
        $web['logo'] = env('app.yuming') . $web['logo'];
        $web['app'] = env('app.yuming') . $web['app'];
        return $web;
    }

}